#include "KDM3Detector.hh"
#include "KDM3HitsCollection.hh"
#include "KDM3Hit.hh"
#include "G4SDManager.hh"
#include "KDM3Globals.hh"
#include <sstream>
#include <iostream>
using namespace std;

KDM3Detector::KDM3Detector(G4String name) :
	G4VSensitiveDetector(name) {
	hitsCollection = 0;
	collectionName.insert("1");
}

KDM3Detector::~KDM3Detector() {
	;
}

void KDM3Detector::Initialize(G4HCofThisEvent* hcc) {
	hitsCollection = new KDM3HitsCollection(SensitiveDetectorName, collectionName[0]);
	G4SDManager* sdMan = G4SDManager::GetSDMpointer();
	G4int collectionID = sdMan->GetCollectionID(hitsCollection);
	hcc->AddHitsCollection(collectionID, hitsCollection);
}

void KDM3Detector::EndOfEvent(G4HCofThisEvent*) {
	;
}

G4bool KDM3Detector::ProcessHits(G4Step *step, G4TouchableHistory*) {
	double deposit = step->GetTotalEnergyDeposit();
	if (deposit == 0.0)
		return false;

	G4Track* track = step->GetTrack();
	double time = track->GetGlobalTime();
	G4ThreeVector pos = track->GetPosition();

	KDM3Hit* hit = new KDM3Hit(time, deposit, pos);
	hitsCollection->Insert(hit);

	return true;
}
